/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.util.UtilFunctions;

/**
 *
 * @author JDK
 */
public class ConjunctionRefinement extends Refinement {

    private String name = new String();
    private ExpandingHistory eh;
    private String corename = new String();
    private String cenclass;
    private String conclass;
    
    public ConjunctionRefinement(String name, String corename, ExpandingHistory eh) {
        this.name = name;
        this.eh = eh;
        this.corename = corename;
        super.refinementType = RefinementType.CONJUNCTION;
    }
    
    public ConjunctionRefinement(ConjunctionRefinement cr) {
        this.name = cr.getName();
        this.eh = cr.getExpandingHistory();
        this.corename = cr.getCoreName();
        this.cenclass = cr.getConditionr();
        this.conclass = cr.getMateConditionr();
                
        super.refinementType = RefinementType.CONJUNCTION;
    }
    
    public ConjunctionRefinement(String prefix, String cen, String con) {
        this.name = prefix + cen + DLConstructors.CONJUNCTION + con;
        this.corename = prefix + cen + DLConstructors.CONJUNCTION + con;
        this.cc = new ConstraintConstructor(cen, prefix, con);
        
        this.cenclass = cen;
        this.conclass = con;
        
        super.refinementType = RefinementType.CONJUNCTION;
    }
    
    public ConjunctionRefinement(String prefix, ExpandingHistory eh, String cen, String con) {
        this.eh = eh;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();        
        this.name = cen + DLConstructors.CONJUNCTION + con;
        this.corename = cen + DLConstructors.CONJUNCTION + con;
        for (int i = 0; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            String p = pa.getProperty();            
            String q = quantifiers.get(i);
            if(pa.getDirection()){
                this.name = q+" "+p+".("+this.name+")";                
            }else{
                this.name = q+" "+p+"￣.("+this.name+")";
            }
            
        }
        
        this.name = prefix + this.name;
        this.cc = new ConstraintConstructor(cen, prefix, con);
        
        this.cenclass = cen;
        this.conclass = con;
        
        super.refinementType = RefinementType.CONJUNCTION;
    }
    
    @Override
    public ExpandingHistory getExpandingHistory(){
        return this.eh;
    }

    @Override
    public void generateRefinementName() {
        super.refinement_name = this.name;
    }
    
    @Override
    public Refinement generateUpdatedRefinement(Vector<TargetInstance> instanceSet){
        Refinement ref = new ConjunctionRefinement(this);
        ref.setBranchingType(this.branching_type);
        ref.setConstraintConstructor(this.cc);
        
        Vector<TargetInstance> oldinsSet = this.getInstanceSet();        
        for (int i = 0; i < instanceSet.size(); i++) {
            TargetInstance ti = instanceSet.get(i);
            if(oldinsSet.contains(ti)){
                ref.setInstanceSet(ti);
            }
        }
//        private boolean refinement_type = true;
//        protected String refinement_name;
//        private Vector<Resource> instanceSet;
        return ref;
    }

    @Override
    public String getCoreName() {
        return this.corename;
    }

    public String getName() {
        return name;
    }

    public String getConditionr() {
        return cenclass;
    }

    public String getMateConditionr() {
        return conclass;
    }
       

    @Override
    public Vector<Resource> getConstraintMatcherInstance(Model model) {
         Vector<Resource> css = new Vector<Resource>();
        
        ResIterator cenres = model.listResourcesWithProperty(RDF.type, this.cenclass);
        ResIterator conres = model.listResourcesWithProperty(RDF.type, this.conclass);
        
        Vector<Resource> cen = UtilFunctions.getSubjectResourceVector(cenres);
        Vector<Resource> con = UtilFunctions.getSubjectResourceVector(conres);

        for (int i = 0; i < cen.size(); i++) {            
            if(con.contains(cen.get(i))){
                if(!css.contains(cen.get(i))){
                    css.add(cen.get(i));                    
                }                
            }
        }        
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return css;
        
    }
}
